package com.jushu.storbox.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.List;

public class FileUtil {

	public static boolean isStringEmputy(String str){
		if(str == null || str.equals("")){
			return true;
		}
		return false;
	}
	
	public static boolean copyFile(String source, String target) {
		if(isStringEmputy(source) || isStringEmputy(target)){
			return false;
		}
		return copyFile(new File(source), new File(target));
	}

	public static boolean copyFile(File source, File target) {
		if(null == source || !source.exists() ){
			return false;
		}
		File parent = target.getParentFile();
		if(!parent.exists()){
			parent.mkdir();
		}
		
		if(!target.exists()){
			try {
				target.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
		}
		FileInputStream is = null;
		FileOutputStream os = null;
		try {
			is = new FileInputStream(source);
			os = new FileOutputStream(target);
			FileChannel iC = is.getChannel();
			FileChannel oC = os.getChannel();
			iC.transferTo(0, iC.size(), oC);
			oC.close();
			iC.close();
			return true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}finally{
			if(is != null){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(os != null){
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static boolean renameFile(String source, String target) {
		File sFile = new File(source);
		File dFile = new File(target);
		if(dFile.exists()) {
			dFile.delete();
		}
		boolean successFLag = false;
		if(sFile.exists()){
			File parent = dFile.getParentFile();
			if(!parent.exists()){
				parent.mkdirs();
			}
			successFLag = sFile.renameTo(dFile);
		}
		return successFLag;
	}
	
	public static boolean renameFile(File source, File target) {
		if(target.exists()) {
			target.delete();
		}
		boolean successFLag = false;
		File parent = target.getParentFile();
		if(parent != null && !parent.exists()){
			parent.mkdirs();
		}
		successFLag = source.renameTo(target);
		return successFLag;
	}
	
	public static boolean moveFile(File source, File target) {
		boolean successFLag = renameFile(source, target);
		if(!successFLag){
			successFLag = copyFile(source, target);
			if(successFLag) {
				source.delete();
			}
		}
		return successFLag;
	}
	
	public static boolean moveFile(String source, String target) {
		boolean successFLag = renameFile(source, target);
		if(!successFLag){
			successFLag = copyFile(source, target);
		}
		return successFLag;
	}

	
	public static boolean targetIsSelfChild(File sourceFolder, File target){
		if(!sourceFolder.exists() || !target.exists() || !sourceFolder.isDirectory()){
			return false;
		}else{
			while(target != null){
				if(target.equals(sourceFolder)){
					return true;
				}else{
					target = target.getParentFile();
				}
			}
			return false;
		}
	}
	
	public static boolean moveSourceToTargetFolder(File source, File target) {
		if(source == null || !source.exists() || target == null || 
				!target.isDirectory() || !target.isDirectory() ) {
			return false;
		}
		
		File sourceParent = source.getParentFile();
		if(sourceParent == null) {
			return false;
		}
		
		if(sourceParent.equals(target)) {
			return true;
		}
		
		if(!target.exists()) {
			if(!target.mkdirs()) {
				return false;
			}
		}
		
		if(source.isDirectory() && FileUtil.targetIsSelfChild(source, target)) {
			return false;
		}
		
		File tempTargetFile = new File(target, source.getName());
		if(source.renameTo(tempTargetFile)) {
			return true;
		}
		
		if(source.isFile()) {
			return copyFile(source, tempTargetFile);
		} else {
			return copyFolder(source, target);
		}
	}
	
	private static boolean copyFolder(File sourceFolder, File targetFolder) {
		if(sourceFolder == null || !sourceFolder.exists()  
				|| targetFolder == null || targetFolder.listFiles() == null) {
			return false;
		}
		
		if(!targetFolder.exists()) {
			if(!targetFolder.mkdirs()) {
				return false;
			}
		} else {
			if(!targetFolder.isDirectory()) {
				return false;
			}
		}
		
		File tempFolder = new File(targetFolder, sourceFolder.getName());
		if(!tempFolder.exists()) {
			if(!tempFolder.mkdirs()){
				return false;
			}
		}
		
		File[] childs = sourceFolder.listFiles();
		if(childs != null) {
			boolean copySuccess = true;
			for(File childFile: childs) {
				if(childFile.isDirectory()) {
					copySuccess &= copyFolder(childFile, tempFolder);
				} else {
					copySuccess &= copyFile(childFile, new File(tempFolder, childFile.getName()));
				}
				
				if(!copySuccess) {
					break;
				}
			}
			return copySuccess;
		} else {
			return false;
		}
	}

	public static void scanFolder(File source, List<File> result) {
		if(source.exists()) {
			if(source.isDirectory()) {
				result.add(source);
				File[] files = source.listFiles();
				if(files != null) {
					for(File f : files) {
						scanFolder(f, result);
					}
				}
			} else {
				result.add(source);
			}
		}
	}
	
	public static void deleteAll(File source) {
		if(!source.exists()) {
			return;
		}
		
		if(source.isFile()) {
			source.delete();
			return ;
		}
		
		if(source.isDirectory()) {
			File[] files = source.listFiles();
			if(files != null) {
				for(File f : files) {
					deleteAll(f);
				}
			}
			source.delete();
		}
		
	}
	
	 /** 
     * get image name by url
     * @param url 
     * @return 
     */  
    public static String getImageName(String url) {  
        String imageName = "";  
        if(url != null){  
            imageName = url.substring(url.lastIndexOf("/") + 1);  
        }  
        return imageName;  
    }  
}
